<?php
include 'sysco.function.php';

$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);

$totOrders = getTotalSales($MERCHANT);
$oloOrders = getTotalSales($MERCHANT, 'online');
$ccOrders = getTotalSales($MERCHANT, 'magcard');
$ppOrders = getTotalSales($MERCHANT, 'paypal');
$cashOrders = getTotalSales($MERCHANT, 'cash');

$_SESSION['from'] = '2012-12-01 03:00:00';
$_SESSION['to'] = '2013-01-01 02:59:00';

$prevTotOrders = getTotalSales($MERCHANT);
$prevOloOrders = getTotalSales($MERCHANT, 'online');
$prevCcOrders = getTotalSales($MERCHANT, 'magcard');
$prevPpOrders = getTotalSales($MERCHANT, 'paypal');

$fp = fopen('reports/sysco_snapshot_old_'.time().'.csv', 'w');

$tmp = array('tot sales', 'olo', 'cc', 'pp', 'cash','tot sale %','olo %','cc %', 'pp %');
fputcsv($fp, $tmp);

$tot_prc = $prevTotOrders > 0 ? (($totOrders-$prevTotOrders)/$prevTotOrders)*100 : 100;
$olo_prc = $prevOloOrders > 0 ? (($oloOrders-$prevOloOrders)/$prevOloOrders)*100 : 100;
$cc_prc = $prevCcOrders > 0 ? ((($ccOrders+$oloOrders)-($prevCcOrders+$prevOloOrders))/($prevCcOrders+$prevOloOrders))*100 : 100;
$pp_prc = $prevPpOrders > 0 ? (($ppOrders-$prevPpOrders)/$prevPpOrders)*100 : 100;

$tmp = array($totOrders, $oloOrders, ($ccOrders+$oloOrders), $ppOrders, $cashOrders, $tot_prc, $olo_prc, $cc_prc, $pp_prc);
fputcsv($fp, $tmp);

$tmp = array('', '');
fputcsv($fp, $tmp);
$tmp = array('cash %', 'PP %', 'cc %');
fputcsv($fp, $tmp);
$tmp = array((($cashOrders/$totOrders)*100), (($ppOrders/$totOrders)*100), ((($ccOrders+$oloOrders)/$totOrders)*100));
fputcsv($fp, $tmp);

$tmp = array('ONLINE %', 'OFFLINE %');
fputcsv($fp, $tmp);
$tmp = array((($oloOrders/$totOrders)*100), ((($totOrders-$oloOrders)/$totOrders)*100));
fputcsv($fp, $tmp);
$tmp = array('-----', '---------');
fputcsv($fp, $tmp);



$amounts = array();
$_SESSION['from'] = '2012-12-26 03:00:00';
$_SESSION['to'] = '2013-02-01 02:59:00';

$totOrders = getTotalSales($MERCHANT);
$totOrderList = $_SESSION['to_orders'];
foreach ($totOrderList as $totOrder) {
  $index = date('m/d', strtotime($totOrder['CREATEDDATE']));
  $amounts[$index] = isset($amounts[$index]) ? $amounts[$index]+ $totOrder['UNITS']*$totOrder['PRICE'] : $totOrder['UNITS']*$totOrder['PRICE'];
}

foreach ($amounts as $key => $amount) {
  fputcsv($fp, array($key, $amount));
}

fclose($fp);
?>